#!/usr/bin/env python
# -*- encoding: utf-8 -*-
'''
@File    :   DeviceAddDialog.py
@Time    :   2020/07/17 14:23:09
@Author  :   Xiong qiufeng
@Version :   1.0
@Contact :   1528399071@qq.com
@License :   (C)Copyright 2017-2018, Liugroup-NLPR-CASIA
@Desc    :   None
'''

from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *

from ui.deviceAdd import Ui_Dialog
from common.libs.Helper import generateUUID

class DeviceAddDialog(QDialog, Ui_Dialog):

    device = {}

    def __init__(self,parent=None):
        super(QDialog, self).__init__(parent)
        self.setupUi(self)
        self.setWindowTitle("添加设备")
        self.pushButton_generateSN.clicked.connect(lambda:self.lineEdit_sn.setText(generateUUID()[0:8]))
        self.lineEdit_sn.textChanged.connect(self.snChanged)

    def snChanged(self):
        self.lineEdit_pub.setText('fsc/%s/%s/tx'%(self.lineEdit_model.text(),self.lineEdit_sn.text()))
        self.lineEdit_sub.setText('fsc/%s/%s/rx'%(self.lineEdit_model.text(),self.lineEdit_sn.text()))

    def load(self, device):
        self.device = device
        self.lineEdit_name.setText(device['value']['name'])
        self.lineEdit_sn.setText(device['value']['sn'])
        self.lineEdit_model.setText(device['value']['model'])
        self.lineEdit_position.setText(device['value']['position'])
        self.lineEdit_pub.setText(device['value']['pub'])
        self.lineEdit_sub.setText(device['value']['sub'])

    def accept(self):
        if self.lineEdit_name.text() == '':
            QMessageBox.information(self,"提示","请输入名称", QMessageBox.Yes)
            return
        if self.lineEdit_position.text() == '':
            QMessageBox.information(self,"提示","请输入位置", QMessageBox.Yes)
            return
        if self.lineEdit_sn.text() == '':
            QMessageBox.information(self,"提示","请输入SN", QMessageBox.Yes)
            return
        if self.lineEdit_model.text() == '':
            QMessageBox.information(self,"提示","请输入型号", QMessageBox.Yes)
            return
        self.device['value']['name'] =  self.lineEdit_name.text()
        self.device['value']['position'] =  self.lineEdit_position.text()
        self.device['value']['sn'] =  self.lineEdit_sn.text()
        self.device['value']['model'] =  self.lineEdit_model.text()
        self.device['value']['pub'] =  self.lineEdit_pub.text()
        self.device['value']['sub'] =  self.lineEdit_sub.text()
        QDialog.accept(self)

    def reject(self):
        QDialog.reject(self)